<!--
 * Copyright 2013 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      // A guest that requests media access.
      // Notifies the embedder about the result of the request (success/fail)
      // via post message. Note that the embedder has to initiate a postMessage
      // first so that guest has a reference to the embedder's window.

      // The window reference of the embedder to send post message reply.
      var embedderWindowChannel = null;

      var notifyEmbedder = function(msg) {
        embedderWindowChannel.postMessage(msg, '*');
      };

      var onUserMediaSuccess = function(stream, testCase) {
        notifyEmbedder(JSON.stringify(['access-granted', testCase]));
      };
      var onUserMediaFailure = function(err, testCase) {
        notifyEmbedder(JSON.stringify(['access-denied', testCase]));
      };
      var startTest = function(testCase) {
        navigator.webkitGetUserMedia(
            {audio: true},
            function(stream) { onUserMediaSuccess(stream, testCase); },
            function(err) { onUserMediaFailure(err, testCase); });
      };
      var onPostMessageReceived = function(e) {
        var data = JSON.parse(e.data);
        var testName = data[0];
        var testCase = data[1];
        if (testName == 'check-media-permission') {
          embedderWindowChannel = e.source;

          // Start the test once we have |embedderWindowChannel|.
          startTest(testCase);
        }
      };
      window.addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>This is a guest that requests audio media.</div>
  </body>
</html>
